import QRCode from 'qrcode';
//import imgSrc from './public/1.jpg'
var name = null;
var to = null;
var ws = null;
document.addEventListener('DOMContentLoaded',function(){
	if(!!!name){
		name = prompt("请输入您的名字");
		to = prompt("接收人的名字");
		ws = new WebSocket('ws://127.0.0.1:8083/'+name);
		ws.onopen = function(){
			ws.onmessage = function(data){
				var info = JSON.parse(data.data);
				displayMsg(info);
			}
			document.querySelector('#send').onclick = function(){
				send(ws,name,to);
			};
		}
	}
});
function displayMsg(data){
	var msgBox = document.querySelector('.msg-box');
	var p = document.createElement('p');
	var msg = `${data.from} 说:${data.data}`;
	p.textContent = msg;
	msgBox.appendChild(p);
}
function send(ws,name,to){
	var msg = document.querySelector('#inputBox');
	var msgObj = {
		from: name,
		to: to,
		data: msg.value
	}
	msg.value = '';
	displayMsg(msgObj);
	ws.send(JSON.stringify(msgObj));
}